// 弹出框
var WfMsg = (function(options) {
    function WfMsg(options) {
        var option = {}
        $.extend(option, this.defaultoption, options)
        console.log(option)
        this.init(option)
    }
    WfMsg.prototype = {
        init: function(option) {
            this.option = option
            this.creatHtml()
        },
        isPC: function() {
            var userAgentInfo = navigator.userAgent;
            var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
            }
            return flag;
        },
        creatHtml: function() {
            var that = this
            var boxposition = this.option.position ? this.option.position : 'top-right'
            if(!this.isPC() && !this.option.position){
                boxposition="center-center"
            }
            if ($('.wfmsgbox.' + boxposition).length == 0) {
                if(!this.isPC()){
                    this.elebox = $('<div class="m-wfmsgbox wfmsgbox ' + boxposition + '"></div>')
                }else{
                    this.elebox = $('<div class="wfmsgbox ' + boxposition + '"></div>')
                }
                $("body").append(this.elebox)
            } else {
                this.elebox = $('.wfmsgbox.' + boxposition)
            }
            if (this.option.shadow) {
                this.shadow = $('<div class="wfmsgshadow"></div>')
                $("body").append(this.shadow)
            }
            this.ele = $('<div class="wfmsg ' + this.option.type + '"><div class="wfcontent">' + this
                .option.content + '</div></div>')
            this.autoclose(this.ele)
            this.elebox.append(this.ele)
            if (this.option.title) {
                var url = this.option.icon[this.option.type]
                if(this.option.showicon){
                    this.ele.prepend(
                        '<div class="wftitle"><span class="icon" style="background-image:url(' + url +
                        ')"></span><span>' + this.option.title + '</span></div>')
                }else{
                    this.ele.prepend(
                        '<div class="wftitle noicon"><span>' + this.option.title + '</span></div>')
                }
            }
            this.events()
        },
        autoclose: function(obj) {
            if (this.option.autoclose) {
                var that = this
                setTimeout(function() {
                    obj.stop(true, true).slideUp(150, function() {
                        var objpar = obj.parent()
                        if (objpar.children("div").length == 1) {
                            obj.remove()
                            objpar.remove()
                        } else {
                            obj.remove()
                        }
                    })
                    if (that.shadow) {
                        that.shadow.stop(true, true).fadeOut(150, function() {
                            that.shadow.remove()
                        })
                    }
                }, that.option.time)
            }
        },
        close: function(obj,time) {
            var that = this
            var hidetime=time?time:0
            setTimeout(function() {
                obj.stop(true, true).slideUp(150, function() {
                    var objpar = obj.parent()
                    if (objpar.children("div").length == 1) {
                        obj.remove()
                        objpar.remove()
                    } else {
                        obj.remove()
                    }
                })
                if (that.shadow) {
                    that.shadow.stop(true, true).fadeOut(150, function() {
                        that.shadow.remove()
                    })
                }
            }, hidetime)
        },
        events: function() {
            var that = this
            if (this.option.shadowclickclose && this.shadow) {
                this.shadow.on("click", function() {
                    that.shadow.stop(true, true).fadeOut(150, function() {
                        that.shadow.remove()
                    })
                    that.ele.stop(true, true).slideUp(150, function() {
                        var objpar = that.ele.parent()
                        objpar.remove()
                    })

                })
            }
            this.ele.on("click", function(e) {
                console.log(e)
                if(that.option.callback){
                    that.option.callback(e,that)
                }
            })
            this.ele.close=function(time){
                that.close(that.ele,time)
            }

        },
        defaultoption: {
            "type": "info", //err,success,warn
            "position": "",
            "title": "",
            "content": "",
            "shadow": "", //true,false
            "shadowclickclose": false,
            "autoclose": false,
            "time": 2000,
            "icon": {
                "info": "",
                "err": "",
                "success": "",
                "warn": ""
            }
        }
    }
    new WfMsg(options)
})
